package org.jeecg.modules.nikeProject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.nikeProject.entity.SportsProject;
import org.jeecg.modules.nikeProject.entity.SportsReference;
import org.jeecg.modules.nikeProject.entity.vo.SportsReferenceVO;
import org.jeecg.modules.nikeProject.mapper.SportsProjectMapper;
import org.jeecg.modules.nikeProject.mapper.SportsReferenceMapper;
import org.jeecg.modules.nikeProject.service.ISportsReferenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;

/**
 * @Description: 申报推荐人
 * @Author: jeecg-boot
 * @Date:   2024-12-11
 * @Version: V1.0
 */
@Service
public class SportsReferenceServiceImpl extends ServiceImpl<SportsReferenceMapper, SportsReference> implements ISportsReferenceService {

    @Autowired
    private SportsProjectMapper sportsProjectMapper;

    @Autowired
    private SportsReferenceMapper sportsReferenceMapper;

    @Override
    public Result<?> add(String username, SportsReferenceVO sportsReferenceVO) {
        String projectId = sportsReferenceVO.getProjectId();
        if (null == projectId || "".equals(projectId)) {
            return Result.error("未查找到案例信息，请联系管理员处理！");
        }

        if (null == sportsReferenceVO.getSportsReferences()){
            return Result.OK("未填写相关的推荐人信息");
        }

        SportsProject sportsProject = sportsProjectMapper.selectById(projectId);
        if (CommonConstant.PROJECT_STEP_FINISH.equals(sportsProject.getProjectStep())){
            return Result.error("案例已提交，不允许编辑!");
        }
        //判断申请推荐人是否存在
        List<SportsReference> sportsReferences = getSportsReferenceByProjectId(projectId);
        if (null != sportsReferences && sportsReferences.size() > 0){
            udpateSportsReferences(sportsReferences, username);
        } else {
            sportsProject.setProjectStep(CommonConstant.PROJECT_STEP_APPLICATION_RECOMMENDER);
            sportsProject.setUpdateTime(new Date());
            sportsProject.setUpdateBy(username);
            sportsProjectMapper.updateById(sportsProject);
        }

        sportsReferences = sportsReferenceVO.getSportsReferences();
        sportsReferences.forEach(x ->{
            x.setProjectId(projectId);
            x.setCreateBy(username);
            x.setCreateTime(new Date());
            sportsReferenceMapper.insert(x);
        });
        return Result.OK("添加成功！");
    }

    public void udpateSportsReferences(List<SportsReference> sportsReferences, String username){
        sportsReferences.forEach(x -> {
            x.setUpdateTime(new Date());
            x.setUpdateBy(username);
            x.setIzActive("1");
            sportsReferenceMapper.updateById(x);
        });
    }

    public List<SportsReference> getSportsReferenceByProjectId(String projectId) {
        LambdaQueryWrapper<SportsReference> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SportsReference::getIzActive, "0");
        lambdaQueryWrapper.eq(SportsReference::getProjectId, projectId);
        return sportsReferenceMapper.selectList(lambdaQueryWrapper);
    }
}
